const PROJECT_PROGRESS = 0;
const GROUP_PROGRESS = 1;
const ATHLETE_PROGRESS = 2;
const NUMBER_PROGRESS = 3;
const CARD_ISSUANCE_PROGRESS = 4;
const SIGN_IN_PROGRESS = 5;
const BATCH_PROGRESS = 6;
const RULE_PROGRESS = 6;
const START_GAME_PROGRESS = 6;
const CALCULATE_SCORE_PROGRESS = 6;
const REPLACE_CARD_PROGRESS = 6;

function updateProgress(progressBarId, nowProgress) {
    if (!window.sessionStorage.getItem("progress") || Number(window.sessionStorage.getItem("progress")) < nowProgress) {
        window.location.replace("index.html");
    }
    let progressBar = document.getElementById(progressBarId);
    let progressNum = 2 + Number(window.sessionStorage.getItem("progress"));
    if (progressNum === 8) {
        for (let child of progressBar.children) {
            child.firstElementChild.removeAttribute("disabled");
        }
    } else {
        for (let child of progressBar.children) {
            if (progressNum !== 0) {
                child.firstElementChild.removeAttribute("disabled");
                progressNum--;
            } else {
                break;
            }
        }
    }

    progressBar.children[0].firstElementChild.addEventListener("click", () => {
        window.location.href = "index.html";
    });
    progressBar.children[1].firstElementChild.addEventListener("click", () => {
        window.location.href = "project.html";
    });
    progressBar.children[2].firstElementChild.addEventListener("click", () => {
        window.location.href = "group.html";
    });
    progressBar.children[3].firstElementChild.addEventListener("click", () => {
        window.location.href = "athlete.html";
    });
    progressBar.children[4].firstElementChild.addEventListener("click", () => {
        window.location.href = "number.html";
    });
    progressBar.children[5].firstElementChild.addEventListener("click", () => {
        window.location.href = "card-issuance.html";
    });
    progressBar.children[6].firstElementChild.addEventListener("click", () => {
        window.location.href = "sign-in.html";
    });
    progressBar.children[7].firstElementChild.addEventListener("click", () => {
        window.location.href = "batch.html";
    });
    progressBar.children[8].firstElementChild.addEventListener("click", () => {
        window.location.href = "rule.html";
    });
    progressBar.children[9].firstElementChild.addEventListener("click", () => {
        window.location.href = "start-game.html";
    });
    progressBar.children[10].firstElementChild.addEventListener("click", () => {
        window.location.href = "calculate-score.html";
    });
}

async function nextProgress(nextProgress, nextPage) {
    let progress = Number(window.sessionStorage.getItem("progress"));
    if (progress < nextProgress) {
        progress++;
        let url = new URL(COMPETITION_URL + "/updateProgress");
        url.searchParams.set("competitionId", window.sessionStorage.getItem("competitionId"));
        url.searchParams.set("progress", String(progress));
        await fetch(url, {
            method: "POST"
        });
        window.sessionStorage.setItem("progress", progress);
    }
    window.location.href = nextPage;
}

async function modifyProgress(nextProgress,html) {
        let url = new URL(COMPETITION_URL + "/updateProgress");
        url.searchParams.set("competitionId", window.sessionStorage.getItem("competitionId"));
        url.searchParams.set("progress", String(nextProgress));
        await fetch(url, {
            method: "POST"
        });
        window.sessionStorage.setItem("progress", nextProgress);
        if(html){
            window.location.href=html
        }
}